package com.onlinevideo.service.impl;

import com.onlinevideo.dao.FavoritesVideoDao;
import com.onlinevideo.dao.HistoryDao;
import com.onlinevideo.dao.VideoDao;
import com.onlinevideo.dao.VideoItemDao;
import com.onlinevideo.dao.impl.FavoritesVideoDaoImpl;
import com.onlinevideo.dao.impl.HistoryDaoImpl;
import com.onlinevideo.dao.impl.VideoDaoImpl;
import com.onlinevideo.dao.impl.VideoItemDaoImpl;
import com.onlinevideo.entity.Page;
import com.onlinevideo.entity.Video;
import com.onlinevideo.entity.VideoItem;
import com.onlinevideo.service.VideoItemService;
import com.onlinevideo.service.VideoService;
import com.onlinevideo.util.FileUpLoadUtils;
import com.onlinevideo.util.PageUtils;
import org.apache.commons.fileupload.FileItem;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class VideoServiceImpl implements VideoService {

    private final VideoDao videoDao = new VideoDaoImpl();
    private final VideoItemDao videoItemDao = new VideoItemDaoImpl();
    private final VideoItemService videoItemService = new VideoItemServiceImpl();
    private final HistoryDao historyDao = new HistoryDaoImpl();
    private final FavoritesVideoDao favoritesVideoDao = new FavoritesVideoDaoImpl();


    @Override
    public int insert(Video video) {
        return videoDao.insert(video);
    }

    @Override
    public int delete(Long id) {
        videoItemService.deleteByVideoId(id);
        String videoImg = videoDao.queryById(id).getVideoImg();
        historyDao.deleteByVideoId(id);
        favoritesVideoDao.deleteByVideoId(id);
        new File(videoImg).delete();
        return videoDao.delete(id);
    }

    @Override
    public int update(Video video) {
        return videoDao.update(video);
    }

    @Override
    public int update(Map<String, Object> videoInfo) {
        String videoName = (String) videoInfo.get("videoName");
        Long videoId = (Long) videoInfo.get("videoId");
        Long categoryId = (Long) videoInfo.get("videoCategoryId");
        String videoDes = (String) videoInfo.get("videoDes");
        List<FileItem> fileItems = (List<FileItem>) videoInfo.get("fileDomain");

        Map<String, Map<String, String>> fileMap = FileUpLoadUtils.uploadFileUtils(fileItems);

        Map<String, String> image = fileMap.get("image");
        Video video = videoDao.queryById(videoId);
        for (String name : image.keySet()) {
            String imgUrl = image.get(name);
            if (!("../uploadFile/images/default.png".equals(video.getVideoImg()))) {
                new File("D:" + video.getVideoImg()).delete();
            }
            video.setVideoImg(imgUrl);
            break;
        }
        if (video == null) {
            return 0;
        }
        video.setCategoryId(categoryId);
        video.setVideoName(videoName);
        video.setVideoDescription(videoDes);
        video.setUploadTime(String.valueOf(System.currentTimeMillis()));
        videoDao.update(video);
        return 0;
    }

    @Override
    public Video queryById(Long id) {
        return videoDao.queryById(id);
    }


    @Override
    public Video queryByIdS1(Long id) {
        return videoDao.queryByIdS1(id);
    }

    @Override
    public Video queryByIdS0(Long id) {
        return videoDao.queryByIdS0(id);
    }

    @Override
    public List<Video> queryAll() {
        return videoDao.queryAll();
    }

    @Override
    public List<Video> queryAllS1() {
        return videoDao.queryAllS1();
    }

    @Override
    public List<Video> queryAllS0() {
        return videoDao.queryAllS0();
    }

    @Override
    public List<Video> queryByName(String name) {
        return videoDao.queryByName(name);
    }

    @Override
    public List<Video> queryByConditions(Video video) {
        return videoDao.queryByConditions(video);
    }

    @Override
    public Page<Video> queryByPage(Integer pageSize, Integer pageNo, Video video) {
        return PageUtils.getPage(pageSize, pageNo, videoDao, video);
    }

    @Override
    public Map<String, Object> queryByPage(String category, String name, Integer pageNo) {
        return videoDao.queryByPageS1(category, name, pageNo);
    }

    public int upload(Map<String, Object> videoInfo) throws Exception {

        List<FileItem> fileDomain = (List<FileItem>) videoInfo.get("fileDomain");

        //处理数据
        Map<String, Map<String, String>> fileMap = FileUpLoadUtils.uploadFileUtils(fileDomain);
        Video video = new Video();
        //生成唯一id
        video.setId(Math.abs(UUID.randomUUID().getLeastSignificantBits()));
        video.setVideoName(String.valueOf(videoInfo.get("videoName")));
        video.setUserId((Long) videoInfo.get("userId"));
        video.setCategoryId((Long) videoInfo.get("categoryId"));
        Map<String, String> image = fileMap.get("image");
        if (image == null || image.size() == 0) {
            video.setVideoImg("../uploadFile/images/default.png");
        } else {
            for (String key : image.keySet()) {
                video.setVideoImg(image.get(key));
                break;
            }
        }
        video.setVideoDescription(String.valueOf(videoInfo.get("videoDescription")));
        video.setUploadTime(String.valueOf(System.currentTimeMillis()));
        video.setVideoStatus(0);
        video.setPlayTotalCount(0);
        video.setVideoAuthority(1);
        video.setCollectionCount(0L);
        //持久化数据到数据库
        int insert = videoDao.insert(video);
        if (insert > 0) {
            Map<String, String> videoItems = fileMap.get("video");
            for (String key : videoItems.keySet()) {
                VideoItem videoItem = new VideoItem();
                videoItem.setVideoId(video.getId());
                videoItem.setUrl(videoItems.get(key));
                String[] s = key.split("_");
                videoItem.setSortCount(Integer.valueOf(s[0]));
                videoItem.setName(s[1]);
                videoItemService.insert(videoItem);
            }
            return 1;
        }
        return 0;
    }


    //
    public int page(Map maps) {

        Map<String, Object> map = queryByPage((String) maps.get("category"), (String) maps.get("videoName"), (Integer) maps.get("pageNo"));
        HttpSession session = (HttpSession) maps.get("session");
        List<Video> videoList = (List<Video>) map.get("videos");
        Integer count = (Integer) map.get("count");
        Integer pageNo = (Integer) map.get("pageNo");
        String category = (String) map.get("category");
        String videoName = (String) map.get("videoName");
        Integer pageSize = (Integer) map.get("pageSize");

        session.setAttribute("videos", videoList);
        session.setAttribute("count", count);
        session.setAttribute("pageNo", pageNo);
        session.setAttribute("category", category);
        session.setAttribute("videoName", videoName);
        session.setAttribute("pageSize", pageSize);

        return 0;
    }


}
